package com.chery.media.helper

import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.PackageManager
import android.location.Address
import android.location.Geocoder
import android.location.LocationManager
import android.os.Build
import androidx.core.app.ActivityCompat
import com.blankj.utilcode.constant.PermissionConstants
import com.blankj.utilcode.util.LogUtils
import com.chery.media.library.extend.explainPermissions
import com.chery.media.library.extend.locationManager
import com.chery.media.library.extend.toJson
import com.chery.media.library.extend.toast

object LocationHelper {

    var latitude = "34.15"

    var longitude = "108.94"

    @SuppressLint("MissingPermission")
    fun getNowLocation(context: Context){
        explainPermissions(PermissionConstants.LOCATION) {
            if (it) {
                // 定位提供者
                var locationProvider = ""
                val providerList = context.locationManager?.getProviders(true)
                if (providerList != null) {
//                    if (providerList.contains(LocationManager.NETWORK_PROVIDER)) {
//                        locationProvider = LocationManager.NETWORK_PROVIDER
//                    } else if (providerList.contains(LocationManager.GPS_PROVIDER)) {
//                        locationProvider = LocationManager.GPS_PROVIDER
//                    }
                    locationProvider = LocationManager.GPS_PROVIDER
                    if (locationProvider.isEmpty().not()) {
                        val location = context.locationManager?.getLastKnownLocation(locationProvider)
                        if (location != null) {
                            latitude = String.format("%.2f", location.latitude)
                            longitude = String.format("%.2f", location.longitude)
                            LogUtils.eTag("LocationHelper", "经纬度====${location.latitude}======${location.longitude}")
                            LogUtils.eTag("LocationHelper", "经纬度====${latitude}======${longitude}")
                        }
                    }
                }
            }
        }
    }

}